<?php
 	include_once('./dbcon.php');

 	$urls_query = $mysqli->query("SELECT id, url FROM urls WHERE title IS NULL OR title = 'Disfruta la música con tu Guitarra! - TCDG' ORDER BY RAND()");

 	while ($url = $urls_query->fetch_assoc()) {
 		$content = file_get_contents($url['url']);

 		$matches = array();
		preg_match('/\<title\>(.*)\<\/title\>/', $content, $matches);
		$title = str_replace('\'', '´', $matches[1]);

 		$matches = array();
		preg_match('/\<h1 class=\"title\"\>(.*)\<\/h1\>/', $content, $matches);
		$subtitle = str_replace('\'', '´', $matches[1]);

		$to_add = ($subtitle == null) ? $title : $subtitle." - ".$title;
		$mysqli->query("UPDATE urls SET title = '".$to_add."' WHERE id = ".$url['id']);
		echo $mysqli->error;
		echo "</br>";
 	}
?>